<?php

namespace Common\Service;


use Think\Exception;

class LogInfoService extends BaseService
{
    public $logModel;
    public function __construct()
    {
        $this->logModel=M('loginfo');
    }

    public function writeLog($type,$info,$data=null,$remark=null){
        try{
            $add['type']=$type;
            $add['info']=$info;
            $add['data']=json_encode($data);
            $add['create_time']=date('Y-m-d H:i:s');
            $add['api']=$_SERVER['REQUEST_URI'];
            $add['remark']=json_encode($remark);
            $this->logModel->add($add);
        }catch (Exception $ex){
            $addEx['type']='Exception';
            $addEx['info']=$ex->getMessage();
            $addEx['data']=json_encode($add);
            $addEx['create_time']=date('Y-m-d H:i:s');
            $add['api']=$_SERVER['REQUEST_URI'];
            $this->logModel->add($addEx);
        }
    }

    /**
     * 查询日志信息
     * @param null $type
     * @param null $id
     * @return mixed
     */
    public function queryLogInfo($type=null,$id=null){
        $where=array();
        if(!empty($type)){
            $where['type']=$type;
        }
        if(!empty($id)){
            $where['id']=$id;
        }
        return $this->logModel->where($where)->select();
    }

    /**
     * 删除日志信息
     * @param null $type
     * @param null $id
     * @return mixed
     */
    public function delLog($type=null,$id=null){
        $where=array();
        if(!empty($type)){
            $where['type']=$type;
        }
        if(!empty($id)){
            $where['id']=$id;
        }
        if(empty($where)){
            return false;
        }
        return $this->logModel->where($where)->delete();
    }

}